/*
编程实现控制台版并支持两人对战的五子棋游戏
* */
import java.util.Scanner;
public class backgammon {
    int row = 17;
    int col = 17;
    String [][] backboard = new String[row][col];
    int step = 0;

    public backgammon() {
        System.out.println("棋盘如下");
        this.draw();
    }

    //绘制棋盘 - 写一个成员方法实现
    public String[][] draw(){
        String[] labels = {"a","b","c","d","e","f"};
        for(int i = 0; i < row; i++){
            for(int j = 0; j < col; j++){
                if (i == 0){
                    if (j > 10){
                        this.backboard[i][j] = labels[j-11];
                    }
                    else if (j == 0) {
                        this.backboard[i][j] = " ";}
                    else {
                        this.backboard[i][j] = String.valueOf(j-1);
                    }
                }
                else{
                    if (j == 0){
                        if (i > 10){
                            this.backboard[i][j] = labels[i-11];
                        }
                        else{
                            this.backboard[i][j] = String.valueOf(i-1);
                        }
                    }
                    else{
                        this.backboard[i][j] = "+";
                    }
                }
            } }
        return this.backboard;
    }
//    提示黑方和白方分别下棋并重新绘制棋盘 - 写一个成员方法实现
//    输入为黑方坐标【行，列】，白方坐标【行，列】
    public String[][] redraw_B(){
//声明变量记录输入是否合格
//        boolean valid = false;
        while (true){
        System.out.println("请输入黑子想要放置的位置行列，以空格隔开，如： 10 2 （这个表示黑子放置在第a行第2列）");
        Scanner sc = new Scanner(System.in);
        String black_input = sc.nextLine();
        String[] black_loc = black_input.split(" ");
        int[] B = this.trs_s_to_int(black_loc);
        if(B[0] > 16 || B[0] < 0 || B[1] > 16 || B[1] < 0){
            System.out.println("该位置超出棋盘范围，请重新输入黑子想要放置的位置行列，以空格隔开，如： 10 2 （这个表示黑子放置在第a行第2列）");
        }
       else{
            if(this.backboard[B[0]][B[1]].equals("+")){
                this.backboard[B[0]][B[1]] = "○";
                System.out.println("已下黑子");
                return this.backboard;
            }
            else{
                System.out.println("该位置已被占，请重新输入正确的位置");
            }
        }
    }
//        return this.backboard;
    }
    public String[][] redraw_W(){
        while (true){
            System.out.println("请输入白子想要放置的位置行列，以空格隔开，如： 1 2 （这个表示白子放置在第1行第2列）");
            Scanner sc = new Scanner(System.in);
            String white_input = sc.nextLine();
            String[] white_loc = white_input.split(" ");
            int[] W = this.trs_s_to_int(white_loc);
            if(W[0] > 16 || W[0] < 0 || W[1] > 16 || W[1] < 0){
                System.out.println("该位置超出棋盘范围，请重新输入白子想要放置的位置行列，以空格隔开，如： 10 2 （这个表示白子放置在第a行第2列）");
            }
            else {
            if(this.backboard[W[0]][W[1]].equals("+")){
            this.backboard[W[0]][W[1]] = "●";
            System.out.println("已下白子");
            return this.backboard;
        }
        else{
            System.out.println("该位置已被占，请输入正确的位置");
        } }}

    }
//    将输入的字符串转为整型
    public int[] trs_s_to_int(String[] s){
        int[] check = new int[2];
        for(int i = 0; i < 2; i++){
            check[i] = Integer.parseInt(s[i])+1;
        }
        return check;
    }

//    将棋盘打印出来
    public void show(){
        for (int i = 0; i < row; i++){
            for (int j = 0; j < col; j ++){
               System.out.print(' '+ this.backboard[i][j] + ' ');
            }
            System.out.print('\n');
        }
    }
    //    每当一方下棋后判断是否获胜 - 写一个成员方法实现。
//    下一次黑子
    public boolean step_B(){
        this.redraw_B();
        this.show();
        this.step++;
        boolean win = this.check_win("B");
        return win;
//        if (this.step >= 5){
//        boolean win = this.check_win("B");
//        return win;
//        }
//        return false;
    }
//    下一次白子
    public boolean step_W(){
        this.redraw_W();
        this.show();
//        if(this.step >= 5){
        boolean win = this.check_win("W");
        return win;/*;}*/
//        return false;
    }
//    检查棋盘是否胜利
    public boolean check_win(String C){
        int i = 1;
//        System.out.println("check now");
        while (i < this.row){
            int j = 1;
//            System.out.println("move in");
            while (j < this.col){
//                System.out.println("move in while inner loop");
                if (C.equals("B")){
                if (this.backboard[i][j].equals("○")){
                   boolean right = this.check_direction("R", i, j);
//                    System.out.println("黑棋right！！");
                    if(right){
                        System.out.println("黑棋胜利！！");
                        return true;
                    }
                    boolean down = this.check_direction("D", i, j);
//                    System.out.println("黑棋down！！");
                    if(down){
                        System.out.println("黑棋胜利！！");
                        return true;
                    }
                    boolean rht_dwn = this.check_direction("RD", i, j);
//                    System.out.println("黑棋right-down！！");
                    if(rht_dwn){
                        System.out.println("黑棋胜利！！");
                        return true;
                    }
                    boolean flt_dwn = this.check_direction("LD", i, j);
                    if(flt_dwn){
                        System.out.println("黑棋胜利！！");
                        return true;
                    }
                }
                    j++;
                }
                else if(C.equals("W")){
                    if(this.backboard[i][j].equals("●")){
                        boolean right = this.check_direction("R", i, j);
//                        System.out.println("白棋right！！");
                        if(right){
                            System.out.println("白棋胜利！！");
                            return true;
                        }
                        boolean down = this.check_direction("D", i, j);
//                        System.out.println("白棋down！！");
                        if(down){
                            System.out.println("白棋胜利！！");
                            return true;
                        }
                        boolean rht_dwn = this.check_direction("RD", i, j);
//                        System.out.println("白棋right-down！！");
                        if(rht_dwn){
                            System.out.println("白棋胜利！！");
                            return true;
                        }
                        boolean flt_dwn = this.check_direction("LD", i, j);
                        if(flt_dwn){
                            System.out.println("白棋胜利！！");
                            return true;
                        }
                    }
                    j++; } }
//            System.out.println("move in inner " + i);
            i++;
        }
//        System.out.println("jump out the while loop");
        return false;
    }
//检查往右/下/右上-左下走是否胜利
    public boolean check_direction(String D, int i, int j){
        int temp = 1;
        int sp = 1;
        if (D.equals("R")){
            while (j < this.col){
                if(j + sp < this.col){
                    String right = this.backboard[i][j+sp];
                    if (this.backboard[i][j].equals(right)){
                        temp++;
                        ++sp;
                        if (temp == 5){
                            return true;
                        }
                    }
                    else {
                        return false;
                    } }
                else {
                    return false;
                }
            } }
        else if (D.equals("D")){
            while (i < this.row){
                if(i + sp < this.row){
                    String down = this.backboard[i+sp][j];
//                    System.out.println("go down");
                    if (this.backboard[i][j].equals(down)){
                        temp++;
                        ++sp;
                        if (temp == 5) {
                            return true;
                        }
                    }
                    else {
                        return false;
                    } }
                else {
                    return false;
                }
            } }
        else if (D.equals("RD")){
            while (i < this.row && j < this.col){
//                System.out.println("jump into rd check i:"+ i+"check j"+ j);
            if(j + sp < this.col && i + sp < this.row){
//                System.out.println("in if condition");
                String rht_dwn = this.backboard[i+sp][j+sp];
                if (this.backboard[i][j].equals(rht_dwn)){
                    temp++;
                    ++sp;
                    if (temp == 5){
                        return true;
                    }
                }
                else {
                    return false;
                } }
            else {
                return false;
            } } }
        else if(D.equals("LD")){
            while (i<this.row && j>=0){
                if(j-sp >=0 && i+sp < this.row){
                    String lft_dwn = this.backboard[i+sp][j-sp];
                    if(this.backboard[i][j].equals(lft_dwn)){
                        temp++;
                        ++sp;
                        if (temp == 5){
                            return true;
                        }
                    }
                    else {
                        return false;
                    } }
                else {
                    return false;
                }
            } }
        return false;
    }}

